package com.lambda.swipeback;

import java.lang.ref.WeakReference;
import java.util.LinkedList;

public class SwipeBackHelper {
    private static LinkedList<WeakReference<SwipeCallback>> sStack = new LinkedList<>();
    private static final String TAG = "MicroMsg.SwipeBackHelper";

    public interface SwipeCallback {
        boolean forceRemoveNoMatchOnPath();

        void onSettle(boolean z, int i);

        void onSwipe(float f);
    }

    public static void push(SwipeCallback aVar) {
        ad.d(TAG, "pushCallback size %d, %s", sStack.size(), aVar.toString());
        sStack.add(0, new WeakReference<>(aVar));
    }

    public static void notifySwipe(float percent) {
        if (sStack.size() <= 0) return;

        SwipeCallback aVar = (SwipeCallback) ((WeakReference) sStack.get(0)).get();
        if (aVar != null) {
            aVar.onSwipe(percent);
        }
    }

    public static boolean pop(SwipeCallback aVar) {
        if (aVar == null) return true;

        ad.d(TAG, "popCallback size %d, %s", sStack.size(), aVar.toString());
        for (int i = 0; i < sStack.size(); i++) {
            if (aVar == ((WeakReference) sStack.get(i)).get()) {
                sStack.remove(i);
                break;
            }
        }
        return false;
    }

    public static void notifySettle(boolean open, int speed) {
        if (sStack.size() <= 0) {
            ad.w(TAG, "notifySettle callback stack empty!, open:%B, speed:%d", open, speed);
            return;
        }
        SwipeCallback aVar = (SwipeCallback) ((WeakReference) sStack.get(0)).get();
        if (aVar == null) {
            ad.w(TAG, "notifySettle null, open:%B, speed:%d", open, speed);
        } else {
            aVar.onSettle(open, speed);
            ad.v(TAG, "notifySettle, open:%B speed:%d callback:%s", open, speed, aVar);
        }
    }
}
